var tm1 = null;
var swiper = null;
var selectColor = 0;

var voice = {
    localId: '',
    serverId: ''
};

var imgs = ['bg.jpg','bo_1.png','card_1.jpg','card_2.jpg','card_3.jpg','bo_2.png','bo_3.png','bo_4.png','bo_5.png',
			'card_front.png','hz_body.png','hz_head.png',
			'logo.png','p1_img.png','p1_t1.png','p1_t2.png','p2_bg.png','p2_bo.png',
			'p2_huatong.png','p2_txt.png','p3_btn_1.png','p3_btn_2.png','p3_btn_3.png',
			'p4_btn_1.png','p4_btn_2.png','p4_btn_3.png','p4_sz_1.png','p4_sz_2.png',
			'p4_sz_3.png','p5_bg_1.jpg','p5_bg_2.png','p5_btn_1.png','p5_btn_2.png',
			'p5_ico1.png','p5_ico2.png','p5_ico3.png','p5_ico4.png','p5_start.png',
			'p5_tree_1.png','p5_tree_2.png','p5_tree_3.png','p5_txt_1.png','p5_txt_2.png','point_down.png','snow_1.png',
			'snow_2.png','snow_3.png','snow_4.png','tree_0.png','tree_1.png',
			'tree_2.png','tree_3.png','share.png'];

$(function() { loadimg(); 
	$('body').on('touchmove',function(event){ event.preventDefault(); });
});

var imgObgList = [];
function loadimg(){
	var c = 0;
	var t = imgs.length;
	for (var im in imgs) {
		var timg = new Image();
		timg.onload = function(){
			c++;
			var per = parseInt(100*c/t);
			$('#per').text(per+'%');
			if(per==100){
				main();
				$('.swiper-container').fadeIn();
				$('#loading').fadeOut();
			}
		}
		timg.src = 'img/'+imgs[im];//+'?t='+new Date().getMilliseconds();
	}
	$('.page1').hide();
}

function main(){
	$('.page1').show();
	setTimeout(function(){ $('.sn1').addClass('snmv'); },0);
	setTimeout(function(){ $('.sn2').addClass('snmv'); },1000);
	setTimeout(function(){ $('.sn3').addClass('snmv'); },2000);
	setTimeout(function(){ $('.sn4').addClass('snmv'); },3000);
	setTimeout(function(){ $('.p5mv_1').addClass('mv'); },1000*Math.random());
	setTimeout(function(){ $('.p5mv_2').addClass('mv'); },1000*(Math.random()+0.4));
	setTimeout(function(){ $('.p5mv_3').addClass('mv'); },1000*(Math.random()+0.8));
	setTimeout(function(){ $('.p5mv_4').addClass('mv'); },1000*(Math.random()+1.2));
	
	swiper = new Swiper('.swiper-container', {
		direction: 'vertical'
		//,initialSlide:3
	});
	$('#nt_btn ').click(function() {
		swiper.slideNext();
	});	
	$('.page3 .p3_btnbox .pbtn_2').click(function(){
		swiper.slidePrev();
		stopbo();
	});
	$('.page3 .p3_btnbox .pbtn_3').click(function(){
		if(voice.localId.length<1){
			alert('请先录音');
			return false;
		}
		swiper.slideNext();
		stopbo();
	});
	 
	$('.page4 .p4_btnbox .pbtn_1').click(function(){
		$('.sz_box').fadeIn();
	});
	$('.page4 .p4_btnbox .pbtn_2').click(function(){
		swiper.slidePrev();
	});
	$('.page4 .p4_btnbox .pbtn_3').click(function(){
		if(selectColor<1){
			alert('请先为树上色！');
			return false;
		}
		resetShare();
		$('.p5_tree').removeClass('p5_tree_1').removeClass('p5_tree_2').removeClass('p5_tree_3').addClass('p5_tree_'+selectColor);
		swiper.slideNext(); 
	});
	$('.sz_box .sz').click(function(){
		selectColor = $(this).attr('d_val');
		$('.sz_box').fadeOut();
		$('.sz_mv').addClass('sz_sel_'+selectColor).show();
		$('.page4 .tree_box .tree').hide();
		$('.page4 .tree_box .tree_0').show(); 
		$('.card').removeClass('card_1').removeClass('card_2').removeClass('card_3').addClass('card_'+selectColor);
		setTimeout(function(){ $('.sz_mv').fadeOut(); },2500);
		setTimeout(function(){ $('.page4 .tree_box .tree_0').fadeOut(); $('.page4 .tree_box .tree_'+selectColor).fadeIn();  },2000);
	});
	
	$('.page5 .p5_btnbox pbtn_2').click(function(){
		$('.page5 .page_set').fadeOut();
		$('.page5 .page_view').fadeIn();
	});
	
	$('.page5 .p5_btnbox .pbtn_1').click(function(){
		swiper.slidePrev();
	});
	$('.page5 .p5_btnbox .pbtn_2').click(function(){
		$('.page5 .page_set').fadeOut();
		$('.page5 .page_view').fadeIn();
		closeCard();
	});
	swiper.on('onSlideChangeStart',function(swiper){
		$('.page1').hide();
	});
	swiper.on('onSlideChangeEnd',function(swiper){
		switch(swiper.activeIndex){
			case 0:
				$('.page1').show();
				break;
		}
	});
	 
};

function playbo(){
	var t = 1;
	tm1 = setInterval(function(){
		$('.page3 .bo_box .bo').hide();
		$('.page3 .bo_box .bo_'+t).show(); 
		t = t%5+1;
	},350);
}

function stopbo(){
	clearInterval(tm1);
}


function closeCard(){
	
	$('.page5 .page_view .card').addClass('mv');
	setTimeout(function(){ $('.page5 .page_view .box_front').fadeIn(); },300);
	setTimeout(function(){ $('.page5 .page_view .p5_txt_2').addClass('mv'); },900);
	setTimeout(function(){ $('.hz').fadeIn();},2000);
	
	$('.p5_txt_2').unbind('touchstart').bind('touchstart',function(){ 
		$('#share').fadeIn(); 
	});
	$('#share').unbind('touchstart').bind('touchstart',function(){ 
		$('#share').fadeOut(); 
	});
}
